import { EnumManager_blxf } from "../Manager/EnumManager";
import PageManager_blxf from "../Manager/PageManager";
import { ConfigModule_blxf } from "../Module/ConfigModule";
import PublicModule_blxf from "../Module/PublicModule";


const {ccclass, property} = cc._decorator;

/** 游戏玩法面板 */

@ccclass
export default class GamePlayPanel_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '游戏玩法节点' })
    gamePlayBox: cc.Node = null;


    // LIFE-CYCLE CALLBACKS:

    @property({ tooltip: '运行帧数' })
    _dtNum: number = 0;


    onLoad () {        
        PublicModule_blxf.gamePlayParentNode = this.gamePlayBox;
    }

    start () {
        this.initEvent_blxf();
    }

    update ( dt: number ) {
        this._dtNum += 1;

        this._dtNum == 5 && this.loadGameBtnList_blxf();
        // 更新自动售卖帧数时，需同步更新重置后运行帧数
        this._dtNum == 10 && this.loadAutoSalePanel_blxf();
        this._dtNum == 60 && this.loadRoomPanel_blxf();

        this.updateOpacity_blxf();
    }

    initEvent_blxf () {
        cc.game.on('onUpdateGamePlayPanel_blxf', this.onUpdateGamePlayPage_blxf, this);

        cc.game.on('onRestartAutoSaclePanel_blxf', this.onRestartAutoSaclePanel_blxf, this);
    }

    onUpdateGamePlayPage_blxf ( isShow: boolean = false ) {
        this.node.x = isShow ? 0 : -99999;
        this.node.opacity = isShow ? 255 : 0;
    }

    /** 加载按钮层 */
    loadGameBtnList_blxf () {
        let _data = {
            pfName: EnumManager_blxf.prefabNameType.PREFAB_GAME_BTN,
            parentNode: this.node
        }
        PageManager_blxf.instance.LoadPrefab_blxf( _data );
    };

    /** 加载自动售卖页面 */
    loadAutoSalePanel_blxf () {
        if ( PublicModule_blxf.isJengaGame ) return;

        let _data = {
            pfName     : EnumManager_blxf.prefabNameType.PREFAB_AUTO_SALE_PANEL,
            parentNode : this.gamePlayBox,
            position   : cc.v2( ConfigModule_blxf.instance.winSizeW, 0 ),
            cb         : ( newNode: cc.Node ) =>{
                PublicModule_blxf.pageNodeInfo[ '2' ] = newNode;
            }
        }
        PageManager_blxf.instance.LoadPrefab_blxf( _data );
    }

    /** 加载房间内装饰 */
    loadRoomPanel_blxf () {
        let _data = {
            pfName     : EnumManager_blxf.prefabNameType.PREFAB_ROOM_PANEL,
            parentNode : this.gamePlayBox,
            // position   : cc.v2( -ConfigModule_blxf.instance.winSizeW, 0 ),
            position   : cc.v2( 0, 0 ),
            cb         : ( newNode: cc.Node ) =>{
                PublicModule_blxf.pageNodeInfo[ '3' ] = newNode;

                PageManager_blxf.instance.loadKitchenPanel_blxf({
                    parentNode: this.gamePlayBox
                });
            }
        }
        PageManager_blxf.instance.LoadPrefab_blxf( _data );
    }

    /** 重新加载自动售卖页面 */
    onRestartAutoSaclePanel_blxf () {
        this._dtNum = 9;
        PublicModule_blxf.pageNodeInfo[ '2' ] && PublicModule_blxf.pageNodeInfo[ '2' ].destroy();
        PublicModule_blxf.pageNodeInfo[ '3' ] && PublicModule_blxf.pageNodeInfo[ '3' ].destroy();
    }

    /** 更新透明度 */
    updateOpacity_blxf () {
        this.gamePlayBox.children.forEach( item =>{
            let _isPage3: boolean = item == PublicModule_blxf.pageNodeInfo[ '3' ];
            let _isCheck: boolean = PublicModule_blxf.gamePlayPageId != 3 && _isPage3;
            let _isItem = item == PublicModule_blxf.pageNodeInfo[ PublicModule_blxf.gamePlayPageId ];
            let _isShow = !_isCheck && !!PublicModule_blxf.pageTween || _isItem;
            if ( _isPage3 ) {
                item.active = _isShow;
            } else {
                item.opacity = _isShow ? 255 : 0;
            }
        })
    }

}
